package com.imedway.ihe.dd.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.imedway.ihe.dd.properties.ItiAddressProperties;
import com.imedway.ihe.dd.properties.ItiAddressProps;
import com.imedway.ihe.dd.properties.ItiServiceProperties;
import com.imedway.ihe.dd.util.QueryRegistryUtil;
import com.imedway.ihe.dd.util.WebserviceUtil;
import com.imedway.ihe.dd.vo.*;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.responses.Response;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.ResponseOptionType;
import org.openehealth.ipf.commons.ihe.xds.iti18.Iti18PortType;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/18
 */
@Slf4j
@RestController("iti-18")
@Api(tags = "[ITI-18]注册表查询")
@RequiredArgsConstructor
public class ITI18Controller {

    private final ItiAddressProperties addressProperties;

    private Iti18PortType getIti18PortType() {
        ItiServiceProperties propertiesIti18 = addressProperties.getIti18();
        ItiAddressProps itiAddressProps = propertiesIti18.getProps();

        return WebserviceUtil.buildWebService(Iti18PortType.class, propertiesIti18.getServiceWsdl(), itiAddressProps);
    }

    private static <T> AdhocQueryRequest getAdhocQueryRequest(Iti18QueryVO<T> iti18QueryVO, QueryRegistry queryRegistry) {
        AdhocQueryRequest request = EbXML30Converters.convert(queryRegistry);
        request.setId(iti18QueryVO.getQueryId());
        request.setComment(iti18QueryVO.getRegistry());
        ResponseOptionType optionType = new ResponseOptionType();
        optionType.setReturnType(iti18QueryVO.getReturnType());
        request.setResponseOption(optionType);
        return request;
    }

    @PostMapping("FindDocuments")
    public Response findDocuments(@RequestBody Iti18QueryVO<FindDocumentsVO> findDocumentsVO) {
        // 构建请求参数
        QueryRegistry findDocumentsQuery = QueryRegistryUtil.buildFindDocumentsQueryRegistry(findDocumentsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(findDocumentsVO, findDocumentsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("FindSubmissionSets")
    public Response findSubmissionSets(@RequestBody Iti18QueryVO<FindSubmissionSetsVO> findSubmissionSetsVO) {
        // 构建请求参数
        QueryRegistry findSubmissionSetsQuery = QueryRegistryUtil.buildFindSubmissionSetsQueryRegistry(findSubmissionSetsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(findSubmissionSetsVO, findSubmissionSetsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("FindFolders")
    public Response findFolders(@RequestBody Iti18QueryVO<FindFoldersVO> findFoldersVO) {
        // 构建请求参数
        QueryRegistry findFoldersQuery = QueryRegistryUtil.buildFindFoldersQueryRegistry(findFoldersVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(findFoldersVO, findFoldersQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetAssociations")
    public Response getAssociations(@RequestBody Iti18QueryVO<GetAssociationsVO> getAssociationsVO) {
        // 构建请求参数
        QueryRegistry getAssociationsQuery = QueryRegistryUtil.buildGetAssociationsQueryRegistry(getAssociationsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getAssociationsVO, getAssociationsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetDocumentsAndAssociations")
    public Response getDocumentsAndAssociations(@RequestBody Iti18QueryVO<GetDocumentsAndAssociationsVO> getDocumentsAndAssociationsVO) {
        // 构建请求参数
        QueryRegistry getDocumentsAndAssociationsQuery = QueryRegistryUtil.buildGetDocumentsAndAssociationsQueryRegistry(getDocumentsAndAssociationsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getDocumentsAndAssociationsVO, getDocumentsAndAssociationsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetSubmissionSets")
    public Response getSubmissionSets(@RequestBody Iti18QueryVO<GetSubmissionSetsVO> getSubmissionSetsVO) {
        // 构建请求参数
        QueryRegistry getSubmissionSetsQuery = QueryRegistryUtil.buildGetSubmissionSetsQueryRegistry(getSubmissionSetsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getSubmissionSetsVO, getSubmissionSetsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetFolderAndContents")
    public Response getFolderAndContents(@RequestBody Iti18QueryVO<GetFolderAndContentsVO> getFolderAndContentsVO) {
        // 构建请求参数
        QueryRegistry getFolderAndContentsQuery = QueryRegistryUtil.buildGetFolderAndContentsQueryRegistry(getFolderAndContentsVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getFolderAndContentsVO, getFolderAndContentsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetFoldersForDocument")
    public Response getFoldersForDocument(@RequestBody Iti18QueryVO<GetFoldersForDocumentVO> getFoldersForDocumentVO) {
        // 构建请求参数
        QueryRegistry getFoldersForDocumentQuery = QueryRegistryUtil.buildGetFoldersForDocumentQueryRegistry(getFoldersForDocumentVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getFoldersForDocumentVO, getFoldersForDocumentQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

    @PostMapping("GetAll")
    public Response getAll(@RequestBody Iti18QueryVO<GetAllVO> getAllVO) {
        // 构建请求参数
        QueryRegistry getAllQuery = QueryRegistryUtil.buildGetAllQueryRegistry(getAllVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(getAllVO, getAllQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getIti18PortType().documentRegistryRegistryStoredQuery(request);

        return EbXML30Converters.convert(queryResponse);
    }

}
































